package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.pojo.TenderInformation;
import com.qf.service.TenderInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tender")
public class TenderInformationController {

    @Autowired
    private TenderInformationService tenderService;

    // 新增
    @PostMapping("/add")
    public boolean add(@RequestBody TenderInformation tender) {
        return tenderService.save(tender);
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id) {
        return tenderService.removeById(id);
    }

    // 修改
    @PutMapping("/update")
    public boolean update(@RequestBody TenderInformation tender) {
        return tenderService.updateById(tender);
    }

    // 主键查询
    @GetMapping("/get/{id}")
    public TenderInformation getById(@PathVariable Integer id) {
        return tenderService.getById(id);
    }

    // 分页查询
    @GetMapping("/page")
    public Page<TenderInformation> page(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return tenderService.page(new Page<>(pageNum, pageSize));
    }

    // 根据招标编号查询
    @GetMapping("/byBusinessCode")
    public TenderInformation byBusinessCode(@RequestParam String businessCode) {
        return tenderService.getOne(
                new QueryWrapper<TenderInformation>().eq("business_code", businessCode)
        );
    }


    // 条件分页查询（示例：按发布时间倒序）
    @GetMapping("/pageByCondition")
    public Page<TenderInformation> pageByCondition(
            @RequestParam String status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return tenderService.page(
                new Page<>(pageNum, pageSize),
                new QueryWrapper<TenderInformation>()
                        .eq("tender_status", status)
                        .orderByDesc("release_time")
        );
    }
}
